package com.sharefang.aproject.presenter;

import android.content.Context;
import android.os.Handler;

import com.sharefang.aproject.BaseApplication;
import com.sharefang.aproject.bean.SelfBean;
import com.sharefang.aproject.bean.UserBean;
import com.sharefang.aproject.biz.user.IUserBiz;
import com.sharefang.aproject.biz.user.OnUserResultListener;
import com.sharefang.aproject.biz.user.UserBiz;
import com.sharefang.aproject.constant.CacheKey;
import com.sharefang.aproject.net.NetApi;
import com.sharefang.aproject.utils.CacheHelper;
import com.sharefang.aproject.view.IUserInfoView;
import com.sharefang.aproject.view.IUserLoginView;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/8/10.
 */

public class UserResultPresenter implements CacheKey, NetApi {
    private IUserBiz userBiz;
    private Context context;
    private IUserLoginView userLoginView;
    private Handler mHandler = new Handler();
    private IUserInfoView userInfoView;

    public UserResultPresenter(Context context, IUserLoginView userLoginView) {
        this.userLoginView = userLoginView;
        this.userBiz = new UserBiz();
        this.context = context;
    }

    public UserResultPresenter(Context context, IUserInfoView userInfoView) {
        this.userBiz = new UserBiz();
        this.userInfoView = userInfoView;
        this.context = context;
    }

    /**
     * 登录
     */
    public void login() {
        userLoginView.showLoading();
        final Map<String, Object> map = new HashMap<>();
        map.put("account", userLoginView.getUserName());
        map.put("password", userLoginView.getPassword());

        userBiz.postUserResult(context, NET_LOGIN, map, new OnUserResultListener() {
            @Override
            public void onSuccess(final UserBean userBean) {
                //需要在UI线程执行

                mHandler.post(new Runnable() {
                    @Override
                    public void run() {

                        CacheHelper.put(LOGINED, "true");
                        SelfBean selfBean = new SelfBean();
                        selfBean.setAccessToken(userBean.getAccessToken());
                        selfBean.setUserId(userBean.getUserId());

                        CacheHelper.put(SELF_BEAN, selfBean);


                        Map<String, Object> userMap = new HashMap<String, Object>();
                        userBiz.getUserResult(context, NET_USER, userMap, new OnUserResultListener() {
                            @Override
                            public void onSuccess(UserBean userBean) {
                                BaseApplication.getInstance().setUserBean(userBean);
                                CacheHelper.put(USER_BEAN, userBean);
                                userLoginView.toMainActivity();
                                userLoginView.hideLoading();
                            }

                            @Override
                            public void onFailed(String message) {

                            }
                        });
                    }
                });

            }

            @Override
            public void onFailed(final String message) {
                //需要在UI线程执行
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        userLoginView.showFailedError(message);
                        userLoginView.hideLoading();
                    }
                });

            }
        });
    }

    /**
     * 获取用户信息
     *
     * @param userId 用户的Id，如果是请求自己的信息，将userId赋值为 -1 即可
     */
    public void getUserInfo(int userId) {
        userBiz = null;
        userBiz = new UserBiz();

        Map<String, Object> map = new HashMap<>();
        if (userId == -1) {
            //获取自己的信息
        } else {
            //获取其他用户的信息
            map.put("userId", userId);
        }

        userBiz.getUserResult(context, NET_USER, map, new OnUserResultListener() {
            @Override
            public void onSuccess(final UserBean userBean) {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        userInfoView.doUserInfo(userBean);
                    }
                });
            }

            @Override
            public void onFailed(final String message) {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        userInfoView.showFailedError(message);
                    }
                });
            }
        });
    }
}
